<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文件上传示例</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Arial', sans-serif;
            background-color: #f7f7f7;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            background-color: #fff;
            border-radius: 8px;
            padding: 40px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 500px;
            text-align: center;
        }

        h1 {
            font-size: 28px;
            margin-bottom: 20px;
            color: #333;
        }

        .file-input-wrapper {
            position: relative;
            width: 100%;
            margin-bottom: 20px;
        }

        /* 自定义上传按钮样式 */
        .file-input {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            opacity: 0;
            cursor: pointer;
        }

        .custom-upload-btn {
            background-color: #4CAF50;
            color: white;
            font-size: 16px;
            padding: 12px 30px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            width: 100%;
            transition: background-color 0.3s ease;
        }

        .custom-upload-btn:hover {
            background-color: #45a049;
        }

        #file-info {
            margin-top: 20px;
            padding: 10px;
            background-color: #f1f1f1;
            border-radius: 5px;
            text-align: left;
        }

        #message {
            font-weight: bold;
            margin-bottom: 10px;
        }

        pre {
            font-size: 14px;
            color: #333;
            background-color: #fff;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            white-space: pre-wrap;
            word-wrap: break-word;
        }
    </style>
</head>

<body>

    <div class="container">
        <h1>文件上传示例</h1>
        <form id="upload-form" enctype="multipart/form-data">
            <div class="file-input-wrapper">
                <input type="file" id="file-input" name="file" class="file-input">
                <button type="button" class="custom-upload-btn">点击选择文件</button>
            </div>
            <button type="submit" class="custom-upload-btn">上传文件</button>
        </form>

        <div id="file-info">
            <p id="message"></p>
            <pre id="file-details"></pre>
        </div>
    </div>

    <script>
        // 判断是否为 FormData 对象
        const isFormData = val => typeof FormData !== 'undefined' && val instanceof FormData;

        document.addEventListener('DOMContentLoaded', function () {
            const form = document.getElementById('upload-form');
            const fileInput = document.getElementById('file-input');
            const fileInfo = document.getElementById('file-info');
            const messageElement = document.getElementById('message');
            const fileDetails = document.getElementById('file-details');
            const customUploadButton = document.querySelector('.custom-upload-btn');

            // 自定义按钮点击时，触发文件选择框
            customUploadButton.addEventListener('click', function () {
                fileInput.click();
            });

            // 监听表单提交
            form.addEventListener('submit', function (event) {
                event.preventDefault(); // 防止页面刷新

                const formData = new FormData(form); // 创建 FormData 对象

                // 判断是否成功创建了 FormData 对象
                if (isFormData(formData)) {
                    messageElement.textContent = 'FormData 对象创建成功！';
                    messageElement.style.color = 'green';

                    // 获取上传的文件并显示信息
                    const file = formData.get('file');
                    if (file) {
                        fileDetails.textContent = `文件名称: ${file.name}\n文件大小: ${file.size} 字节\n文件类型: ${file.type}`;
                    } else {
                        fileDetails.textContent = '未选择文件。';
                    }
                } else {
                    messageElement.textContent = '创建 FormData 对象失败。';
                    messageElement.style.color = 'red';
                }
            });
        });
    </script>

</body>

</html>